import java.util.Vector;


public class OurInputClass 
{
	private static Vector<Component> comps;
	public static Vector<Component> tree = new Vector<Component>();
	public static Vector<Component> leaves = new Vector<Component>();
	
	public static CoverageMatrix covMat;
	public static Vector<Test> tests;
	public static Component head;
	
	/**
	 * builds a random DAG according to comps
	 * @param comps - the components of which the DAG is built
	 */
	public static void BuildRandomGraphFromComps(Vector<Component> comps)
	{
		OurInputClass.comps = comps;
		Vector<Component> tempComps = new Vector<Component>(comps);
		if(!tempComps.isEmpty())
		{
			tree.add(tempComps.remove(0));
			head = tree.firstElement();
			leaves.add(head);
		}
		else
			return;
		
		while(!tempComps.isEmpty())
		{
			
			Component curr = tempComps.remove(0);
			Component rand = tree.elementAt((int)(Math.random()*tree.size()));
			rand.addSuccessor(curr);
			
			tree.add(curr);
			leaves.add(curr);
			leaves.remove(rand);
		}
	}
	
	/**
	 * deduce the coverage matrix according to the graph.
	 */
	public static void BuildCovrageMatrix()
	{
		int[][] arr = new int[leaves.size()][tree.size()];
		
		for (int i = 0; i < leaves.size(); i++) 
		{
			int[] line = new int[tree.size()];
			
			Component curr = leaves.elementAt(i);
			while(curr != null)
			{
				line[curr.getId()] = 1;
				curr = curr.getPredecessor();
			}
			arr[i] = line;
		}
		covMat = new CoverageMatrix(arr, arr.length, arr[0].length);
	}
	
	/**
	 * builds the tests for each monitored component
	 */
	public static void BuildTests()
	{
		tests = new Vector<Test>();
		for (int i = 0; i < leaves.size(); i++) 
		{
			final Component toTest = leaves.elementAt(i); 
			tests.add(new Test(i) {
				
				@Override
				protected boolean runTest(Vector<Component> componentsSet) 
				{
					return TestSimulation.RunTest(toTest);
				}
			});
			tests.lastElement().runTest(null);
		}
	}

	/**
	 * initialize all static fields of this class to default values.
	 */
	public static void restartInput()
	{
		comps = null;
		tree = new Vector<Component>();
		leaves = new Vector<Component>();
		
		covMat = null;
		tests = null;
		head = null;
	}
	
	
}
